package com.jcl.recruit.api.resume.controller;

import com.jcl.core.resp.ApiResponse;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.core.constants.UserExternalLoginConstant;
import com.jcl.recruit.core.web.utils.RequestUtils;
import com.jcl.recruit.resume.service.ResumeDownloadService;
import com.jcl.recruit.user.model.UserExternalAccess;
import com.jcl.recruit.user.service.UserExternalAccessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName ResumeChannelController
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/11 16:23
 * @Version 1.0
 **/
@RequestMapping("/api")
@RestController
public class ResumeDownloadController {

    @Autowired
    private UserExternalAccessService    userExternalAccessService;

    @Autowired
    private ResumeDownloadService        resumeDownloadService;

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 15:59 2018/10/19
     * @Param [id]
     * @return com.example.util.ApiResponse
     **/
    @RequestMapping("/resume/channel/download/{id}")
    public ApiResponse resumeDownload(@PathVariable("id") String id){

        String corpCode = RequestUtils.getCorpCode();

        if(Constants.SubcompanyInfo.DEFAULT_TRY_CODE.equals(corpCode)){
            UserExternalAccess access = userExternalAccessService.getUserExternalAccessByUserId(
                    RequestUtils.getUserId(), UserExternalLoginConstant.USER_ACCESS_CATEGORY_WECHAT);
            Map<String, String> dataMap = new HashMap<>();
            if(null != access){

                dataMap.put("userKey", access.getExternalKey());
            }
            return ApiResponse.warning("请注册公司", Constants.ReturnInfo.WARNING_CODE_REGISTER, dataMap);
        }

        Map<String, String> dataMap= resumeDownloadService.download(Integer.parseInt(id));
        if(dataMap.size() > 0){
            return ApiResponse.error(dataMap.get("msg"));
        }
        return ApiResponse.success("下载中，稍后查看...");
    }


}
